package com.yuezhu.Model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by xuxingcan on 2016/6/14.
 */
public class PlanModel {
    public class Area implements Comparable<Area> {
        public long createAt;
        public String areaId;
        public String bldgId;
        public String planId;
        public String areaName;
        public int floorBottom;
        public int floorTop;
        public int type;
        public int areaGrade;
        public int skyLobby;
        public int floorHeight;
        public int floorValue;
        public double liftSpeed;
        public int liftLoad;
        public int liftNum;
        public int areaIndex;
        public int persionRatio;
        public int targetPerson;

        public boolean isSkyLobby() {
            return skyLobby == 1;
        }

        public void setIsSkyLobby(boolean is) {
            skyLobby = is ? 1 : 0;
        }

        @Override
        public int compareTo(Area area) {
            return areaIndex - area.areaIndex;
        }
    }

    public class Plan implements Comparable<Plan> {
        public long createAt;
        public String bldgId;
        public int planAreaNum;
        public String planId;

        @Override
        public int compareTo(Plan plan) {
            return planAreaNum - plan.planAreaNum;
        }
    }

    public ArrayList<Plan> planArrayList = new ArrayList<>();
    public ArrayList<Area> areaArrayList = new ArrayList<>();

    public PlanModel(JSONObject json) throws JSONException {
        Gson gson = new Gson();
        planArrayList = gson.fromJson(json.getJSONArray("bldgPlanlist").toString(), new TypeToken<List<Plan>>() {
        }.getType());
        areaArrayList = gson.fromJson(json.getJSONArray("list").toString(), new TypeToken<List<Area>>() {
        }.getType());
        for (Plan p : planArrayList) {
            int count = 0;
            for (Area area : areaArrayList) {
                if (area.planId.equalsIgnoreCase(p.planId)) {
                    count++;
                }
            }
            p.planAreaNum = count;
        }
    }

    public ArrayList<String> getPlanNumList() {
        ArrayList<String> list = new ArrayList<>();
        for (Plan p : planArrayList) {
            list.add(p.planAreaNum + "");
        }
        return list;
    }

    public ArrayList<Area> getAreaArrayListByPlanId(String planId) {
        ArrayList<Area> list = new ArrayList<>();
        for (Area area : areaArrayList) {
            if (area.planId.equalsIgnoreCase(planId)) {
                list.add(area);
            }
        }
        return list;
    }
}
